#include "../include/alphadog_monitor/login_window.h"

#include <QMovie>
#include <QtWidgets/QAbstractButton>
#include <QNetworkInterface>
#include <QMessageBox>
#include <QSettings>

#include "../include/alphadog_monitor/main_window.hpp"
#include "ui_login_window.h"


 login_window::login_window(QWidget *parent):
      CustomMoveWidget(parent),
      ui(new Ui::login_window)
  {
      // 创建UI界面
      ui->setupUi(this);

      // 初始化控件
      InitWidget();

      // 播放动画
      QMovie* movie = new QMovie("://background/background.gif");
      ui->label_video->setMovie(movie);
      movie->start();

      // 查询IP
      foreach (QHostAddress address, QNetworkInterface::allAddresses())
      {
        if (address.protocol() == QAbstractSocket::IPv4Protocol)
        {
          QString addre = address.toString();
          if (addre.split(".")[0] == "192") {
            m_qRosIp = addre;
            m_qMasterIp = "http://" + addre + ":11311";
          } else if (addre.split(".")[0] == "10") {
            m_qRosIp = addre;
            m_qMasterIp = "http://" + addre + ":11311";
          } else if (addre.split(".")[0] == "172") {
            m_qRosIp = addre;
            m_qMasterIp = "http://" + addre + ":11311";
          }
        }
      }

      // 检测本机ID
      connect(ui->btnAuto, &QPushButton::clicked, [=]() {
        ui->lineEditMasterIp->setText(m_qMasterIp);
        ui->lineEditRosIp->setText(m_qRosIp);
      });

      // 说明信息
      connect(ui->btnIntroduce, &QPushButton::clicked, [=]() {
          QMessageBox::information(NULL, "信息",
                                   "版本号：1.0\n开发者：华南理工大学机器人实验室",
                                   QMessageBox::Yes);
      });

      // 设置
      connect(ui->btnSettings, &QPushButton::clicked, [=]() {
          setinf->Display(0);
      });

      // 最小化和关闭
      connect(ui->btnWinClose_1, &QPushButton::clicked, [=]() { this->close(); });
      connect(ui->btnWinMin_1, &QPushButton::clicked, [=]() { this->showMinimized(); });
  }

login_window::~login_window()
{
    delete ui;
}

/**
 * @brief 用户登录
 */
void login_window::on_btnLogin_clicked(void)
{
    QString s = ui->btnLogin->text();
    if (ui->btnLogin->text() != "&CANCEL")
    {
      ConnectMaster();
    }
    else
    {
      ui->btnLogin->setText("CONNECT");
      ui->btnLogin->setStyleSheet(
          "border:0px;background-color:#F81243;color:WHITE；");
      m_bIsConnect = false;
    }
}

/**
 * @brief 连接ROS主节点
 */
void login_window::ConnectMaster(void) {
  int argc = 0;
  char** argv = NULL;

  // 创新新的主界面
  if (mainWindow != NULL)
  {
    mainWindow->close();
  }
  mainWindow = new alphadog_monitor::MainWindow(argc, argv);
  connect(mainWindow, SIGNAL(signalDisconnect()), this,
          SLOT(slot_ShowWindow()));
  QCoreApplication::processEvents();
  ui->btnLogin->setText("CANCEL");
  ui->btnLogin->setStyleSheet(
              "border:0px;background-color:rgb(211, 215, 207);color:WHITE；");

  // 尝试连接主节点
  bool isConnect = mainWindow->connectMaster(ui->lineEditMasterIp->text(), ui->lineEditRosIp->text());
//  bool isConnect = mainWindow->connectMaster("http://192.168.137.181:11311", ui->lineEditRosIp->text());

  // 连接成功后，显示主界面
  if (isConnect)
  {
    // 将设置写入数据库
    QSettings connect_info("alphadog_monitor", "connect_info");
    connect_info.setValue("master_url", ui->lineEditMasterIp->text());
    connect_info.setValue("host_url", ui->lineEditRosIp->text());
    ui->btnLogin->setText("CONNECT");
    ui->btnLogin->setStyleSheet(
        "border:0px;background-color:#F81243;color:WHITE；");
    this->hide();
    mainWindow->Display();

  // 连接失败，返回错误信息
  }
  else
  {
    ui->btnLogin->setText("CONNECT");
    ui->btnLogin->setStyleSheet(
        "border:0px;background-color:#F81243;color:WHITE；");
    QMessageBox::warning(NULL, "连接失败",
                             "连接失败！请检查你的连接配置或重启重试！",
                             QMessageBox::Yes);
  }
}

/**
 * @brief 自动登录
 */
void login_window::slot_autoLoad()
{
//    if (m_bIsConnect)
//    {
//        ConnectMaster();
//    }
}

/**
 * @brief 显示窗口
 */
void login_window::slot_ShowWindow()
{
    this->show();
    ui->btnLogin->setEnabled(true);
    ui->btnLogin->setStyleSheet(
        "border:0px;background-color:#F81243;color:WHITE；");
}

/**
 * @brief 写入设置信息
 */
void login_window::slot_writeSettings(void)
{}

/**
 * @brief 初始化控件
 */
void login_window::InitWidget(void)
{
    // 设置
    setinf = new Setting;

    // 信号槽
    connect(ui->lineEditRosIp, SIGNAL(returnPressed()), this,
            SLOT(on_btnLogin_clicked()));
    connect(ui->lineEditMasterIp, SIGNAL(returnPressed()), this,
            SLOT(on_btnLogin_clicked()));

    // 读取设置信息
    readSettings();
}


/**
 * @brief 读取设置信息
 */
void login_window::readSettings(void)
{
    // 登录界面
    QSettings connect_info("alphadog_monitor", "connect_info");
    ui->lineEditMasterIp->setText(
        connect_info.value("master_url", m_qMasterIp).toString());

    ui->lineEditRosIp->setText(
        connect_info.value("host_url", m_qRosIp).toString());
}


